//
//  FindLoginCodeViewController.m
//  ZYZB
//
//  Created by ENERGY on 2018/7/16.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "FindLoginCodeViewController.h"

static int i = 59;
@interface FindLoginCodeViewController ()<UITextFieldDelegate>
@property (nonatomic, strong)UITextField * phoneTextField;//电话号码
@property (nonatomic, strong)UITextField * verificationCodeTextField;//验证码
@property (nonatomic, strong)UIButton * getVerificationCodeBtn;//验证码按钮
@property (nonatomic, strong)UITextField * loginTextField;//登录密码
@property (nonatomic, strong)UITextField * loginAgainTextField;//再次登录密码
@property (nonatomic, strong)NSString * telNumStr;//电话号码
@property (nonatomic, strong)NSTimer *timmer;
@end

@implementation FindLoginCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setUpViews];
    self.navigationItem.title = @"找回登录密码";
}

- (void)setUpViews{
    //手机图案
    int phoneImgX = 50/WIDTH_6_SCALE;
    UIImageView * phoneImg = [[UIImageView alloc] init];
    if (Is_Iphone_X) {
        phoneImg.frame = CGRectMake(phoneImgX, 36/WIDTH_6_SCALE + 24, 12/WIDTH_6_SCALE, 20/WIDTH_6_SCALE);
    }else{
        phoneImg.frame = CGRectMake(phoneImgX, 36/WIDTH_6_SCALE, 12/WIDTH_6_SCALE, 20/WIDTH_6_SCALE);
    }
    phoneImg.image = [UIImage imageNamed:@"phone"];
    [self.view addSubview:phoneImg];
    //上面的输入框
    self.phoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(phoneImg.right + 12/WIDTH_6_SCALE, 0, SCREEN_WIDTH - phoneImg.right - 12/WIDTH_6_SCALE - phoneImg.left, 22/WIDTH_6_SCALE)];
    self.phoneTextField.delegate = self;
    self.phoneTextField.centerY = phoneImg.centerY;
    self.phoneTextField.font = DEF_FontSize_12;
    self.phoneTextField.placeholder = @"请输入手机号码";
    self.phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.phoneTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:self.phoneTextField];
    //上面横线
    UIView * topLineView = [[UIView alloc] initWithFrame:CGRectMake(phoneImg.left, phoneImg.bottom + 8/WIDTH_6_SCALE, SCREEN_WIDTH - phoneImgX*2, 1/WIDTH_6_SCALE)];
    topLineView.backgroundColor = getColor(grayColor);
    [self.view addSubview:topLineView];
    //锁头图案
    UIImageView * lockImg = [[UIImageView alloc] initWithFrame:CGRectMake(phoneImg.left, topLineView.bottom + 32/WIDTH_6_SCALE, 17.5/WIDTH_6_SCALE, 18/WIDTH_6_SCALE)];
    lockImg.centerX = phoneImg.centerX;
    lockImg.image = [UIImage imageNamed:@"verificationCode"];
    [self.view addSubview:lockImg];
    //下面输入框
    self.verificationCodeTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.phoneTextField.left, 0, self.phoneTextField.width, self.phoneTextField.height)];
    self.verificationCodeTextField.centerY = lockImg.centerY;
    self.verificationCodeTextField.delegate = self;
    self.verificationCodeTextField.font = DEF_FontSize_12;
    self.verificationCodeTextField.placeholder = @"请输入验证码";
    [self.view addSubview:self.verificationCodeTextField];
    //获取验证码按钮
    int getVerificationCodeBtnW = 90/WIDTH_6_SCALE;
    self.getVerificationCodeBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.phoneTextField.right - getVerificationCodeBtnW, 0, getVerificationCodeBtnW, 30/WIDTH_6_SCALE)];
    self.getVerificationCodeBtn.centerY = self.verificationCodeTextField.centerY;
    [self.getVerificationCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.getVerificationCodeBtn addTarget:self action:@selector(getVerificationCodeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.getVerificationCodeBtn setBackgroundColor:[UIColor orangeColor]];
    [self button:self.getVerificationCodeBtn withBGColor:[UIColor orangeColor] withLayerBorderColor:nil withLayerBorderWith:0 withCornerRadius:6 withMasksToBounds:YES withViwe:self.view];
    [self.getVerificationCodeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.getVerificationCodeBtn.titleLabel.font = DEF_FontSize_12;
    [self.view addSubview:self.getVerificationCodeBtn];
    //下面横线
    UIView * bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(topLineView.left, lockImg.bottom + 8/WIDTH_6_SCALE, topLineView.width, topLineView.height)];
    bottomLineView.backgroundColor = getColor(grayColor);
    [self.view addSubview:bottomLineView];
    //密码锁头1图案
    UIImageView * lockOneImg = [[UIImageView alloc] initWithFrame:CGRectMake(phoneImg.left, bottomLineView.bottom + 32/WIDTH_6_SCALE, 16/WIDTH_6_SCALE, 18/WIDTH_6_SCALE)];
    lockOneImg.centerX = phoneImg.centerX;
    lockOneImg.image = [UIImage imageNamed:@"code"];
    [self.view addSubview:lockOneImg];
    //最下面输入框
    self.loginTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.phoneTextField.left, 0, self.phoneTextField.width, self.phoneTextField.height)];
    self.loginTextField.centerY = lockOneImg.centerY;
    self.loginTextField.delegate = self;
    self.loginTextField.font = DEF_FontSize_12;
    self.loginTextField.placeholder = @"请输入登录密码";
    self.loginTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:self.loginTextField];
    //最下面横线
    UIView * downLineView = [[UIView alloc] initWithFrame:CGRectMake(topLineView.left, lockOneImg.bottom + 8/WIDTH_6_SCALE, topLineView.width, topLineView.height)];
    downLineView.backgroundColor = getColor(grayColor);
    [self.view addSubview:downLineView];
    //密码锁头2图案
    UIImageView * lockTwoImg = [[UIImageView alloc] initWithFrame:CGRectMake(phoneImg.left, downLineView.bottom + 32/WIDTH_6_SCALE, 16/WIDTH_6_SCALE, 18/WIDTH_6_SCALE)];
    lockTwoImg.centerX = phoneImg.centerX;
    lockTwoImg.image = [UIImage imageNamed:@"code"];
    [self.view addSubview:lockTwoImg];
    //最下面输入框
    self.loginAgainTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.phoneTextField.left, 0, self.phoneTextField.width, self.phoneTextField.height)];
    self.loginAgainTextField.centerY = lockTwoImg.centerY;
    self.loginAgainTextField.delegate = self;
    self.loginAgainTextField.font = DEF_FontSize_12;
    self.loginAgainTextField.placeholder = @"请输入确认密码";
    self.loginAgainTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:self.loginAgainTextField];
    //最下面横线
    UIView * lowLineView = [[UIView alloc] initWithFrame:CGRectMake(topLineView.left, lockTwoImg.bottom + 8/WIDTH_6_SCALE, topLineView.width, topLineView.height)];
    lowLineView.backgroundColor = getColor(grayColor);
    [self.view addSubview:lowLineView];
    //密码说明
    UILabel * introduceLab = [[UILabel alloc] initWithFrame:CGRectMake(lowLineView.left, lowLineView.bottom + 17/WIDTH_6_SCALE, lowLineView.width, 10/WIDTH_6_SCALE)];
    [self label:introduceLab withFont:DEF_FontSize_10 withText:@"密码为6-20位，由数字、字母和符号三种组成" withTextColor:getColor(@"cbcbcb") withTextAlignment:NSTextAlignmentLeft withView:self.view];
    //提交
    UIButton * submitBtn = [[UIButton alloc] initWithFrame:CGRectMake(lowLineView.left, introduceLab.bottom + 36/WIDTH_6_SCALE, SCREEN_WIDTH - lowLineView.left * 2, 37/WIDTH_6_SCALE)];
    [self button:submitBtn withBGColor:getColor(@"3675bd") withLayerBorderColor:nil withLayerBorderWith:0 withCornerRadius:6 withMasksToBounds:YES withViwe:self.view];
    [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    submitBtn.titleLabel.font = DEF_FontSize_14;
    [submitBtn addTarget:self action:@selector(submitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - action
- (void)getVerificationCodeBtnClick:(UIButton *)sender{
    NSLog(@"发送验证码");
    if ([self valiMobile:self.phoneTextField.text]) {
        self.telNumStr = self.phoneTextField.text;
//        //获取验证码
//        [self getNetVerificationCode];
        //初始化定时器
        _timmer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(test:) userInfo:nil repeats:YES];
    }else{
        [self.view makeToast:@"请正确填写手机号码" duration:1 position:CSToastPositionCenter style:nil];
        return;
    }
}
- (void)test:(NSTimer *)timmer{
    NSString *str = [NSString stringWithFormat:@"%d后重试",i];
    self.getVerificationCodeBtn.titleLabel.text = str;
    self.getVerificationCodeBtn.userInteractionEnabled = NO;
    i--;
    if (i == -1) {
        [timmer invalidate];
        self.getVerificationCodeBtn.titleLabel.text = @"获取验证码";
        self.getVerificationCodeBtn.userInteractionEnabled = YES;
        i = 59;
    }
}
- (void)submitBtnClick:(UIButton *)sender{
    NSLog(@"提交");
    if (self.phoneTextField.text.length < 11) {
        [self.view makeToast:@"请正确填写手机号码" duration:1 position:CSToastPositionCenter style:nil];
        return;
    }
    if ([self valiMobile:self.phoneTextField.text]) {
        NSLog(@"成功");
    }else{
        [self.view makeToast:@"请正确填写手机号码" duration:1 position:CSToastPositionCenter style:nil];
        return;
    }
}
- (void)textFieldDidChange :(UITextField *)theTextField{
    if (self.phoneTextField) {
        if (self.phoneTextField.text.length == 11) {
            if ([self valiMobile:self.phoneTextField.text]) {
                NSLog(@"%@", self.phoneTextField.text);
            }else{
                [self.view makeToast:@"请正确填写手机号码" duration:1 position:CSToastPositionTop style:nil];
                return;
            }
        }
        if (self.phoneTextField.text.length > 11) {
            [self.view makeToast:@"请正确填写手机号码" duration:1 position:CSToastPositionTop style:nil];
            return;
        }
    }
}
@end
